/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.controllers.events;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import org.toedo.logging.Log;

public class EventController {

	/**
	 * Holder for singleton implementation.
	 */
	private static class EventControllerHolder {
		private static final EventController INSTANCE = new EventController();
	}
	
	/**
	 * Get the unique instance of the DataController.
	 * @return The instance of the DataController
	 */
	public static EventController getInstance() {
		return EventControllerHolder.INSTANCE;
	}
	
	protected transient PropertyChangeSupport generalEventsListeners;
	
	private boolean mBoEventsPropagationEnabled = true;
	
	private EventController() {
		generalEventsListeners = new PropertyChangeSupport(this);
		
		Log.getInstance(EventController.class).info("EventController initialized."); //$NON-NLS-1$
	}
	
	public void addGeneralEventsListener(PropertyChangeListener l){
        if (l == null) {
            throw new IllegalArgumentException();
        }
        this.generalEventsListeners.addPropertyChangeListener(l);        
    }
    
    public void removeGeneralEventsListener(PropertyChangeListener l){
        this.generalEventsListeners.removePropertyChangeListener(l);
    }
    
    public void fireGeneralEvent(String prop, Object old, Object newValue){
        if ((this.generalEventsListeners.hasListeners(prop)) &&
			(mBoEventsPropagationEnabled)) {
            this.generalEventsListeners.firePropertyChange(prop, old, newValue);
        }
    }
    
    public void setEventPropagationEnabled(boolean value) {
    	mBoEventsPropagationEnabled = value;
    }
	
}
